package org.example;
import com.github.pjfanning.xlsx.StreamingReader;
import org.apache.poi.ss.usermodel.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class PjfanningXlsxStreamReader {
    public static void main(String[] args) throws FileNotFoundException {
        String filePath = "E:\\项目文件\\记录\\20241203 微信通道接入\\REFUND.xlsx";
        FileInputStream in = new FileInputStream(filePath);
        readBigExcel(in);
    }
    private static void readBigExcel(InputStream in) throws FileNotFoundException {
        long start = System.currentTimeMillis();
        try (Workbook workbook = StreamingReader.builder()
                .rowCacheSize(100)    // 缓存的行数
                .bufferSize(4096)     // 输入流的缓冲区大小
                .open(in)) {         // 打开文件输入流
            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
            System.out.println("\n"+(System.currentTimeMillis()-start));

            for (Row row : sheet) {
                for (Cell cell : row) {
                    switch (cell.getCellType()) {
                        case STRING:
                            System.out.print(cell.getStringCellValue() + "\t");
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                System.out.print(cell.getDateCellValue() + "\t");
                            } else {
                                System.out.print(cell.getNumericCellValue() + "\t");
                            }
                            break;
                        case BOOLEAN:
                            System.out.print(cell.getBooleanCellValue() + "\t");
                            break;
                        case FORMULA:
                            System.out.print(cell.getCellFormula() + "\t");
                            break;
                        default:
                            System.out.print("?\t");
                            break;
                    }
                }
                System.out.println("\n"+(System.currentTimeMillis()-start));
                System.out.println("\n"+(System.currentTimeMillis()-start));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
